package com.upbos.upm.dict;

import cn.hutool.core.util.ReflectUtil;
import com.upbos.upm.context.SpringContext;
import com.upbos.upm.entity.DictItem;
import com.upbos.upm.entity.DictMapping;

import java.util.List;
import java.util.Map;

/**
 * <b>Application name：</b> DictUtil.java <br>
 * <b>Application describing: 数据字典翻译工具</b> <br>
 * <b>Copyright：</b> Copyright &copy; 2015-2020 myzh.com 版权所有。<br>
 * <b>Company：</b> myzh.com <br>
 * <b>Date：</b> 2019年04月18日 17:39 <br>
 * <b>author：</b> <a href="mailto:wjzchina2008@126.com"> Jason </a>
 * <b>version：</b>V3.0.0
 *
 * @author wangjz
 */
public class DictUtil {
    /**
     * 对数据进行字典翻译，可以翻译多组字典
     *
     * @param data     待翻译的数据
     * @param mappings 翻译映射关系
     * @return
     * @throws Exception
     */
    public static Object translate(Object data, DictMapping[] mappings) throws Exception {
        if (mappings == null) {
            return data;
        }

        for (DictMapping mapping : mappings) {
            translate(data, mapping);
        }

        return data;
    }


    public static Object translate(Object data, DictMapping mapping) throws Exception {
        DictService dictService = SpringContext.getBean("dictService");
        List<DictItem> dictItems = dictService.listDictItemByDictKey(mapping.getDictKey());
        translate(dictItems, data, mapping);
        return data;
    }


    /**
     * 对数据进行字典翻译
     *
     * @param dictItems   数据字典
     * @param data    待翻译的数据
     * @param mapping 翻译映射关系
     * @return 翻译后的数据
     * @throws Exception
     */
    private static Object translate(List<DictItem> dictItems, Object data, DictMapping mapping) throws Exception {
        if (data == null) {
            return data;
        }

        if (data instanceof Map) {
            String value = getValue(dictItems, (String) ((Map) data).get(mapping.getSourceProp()));
            ((Map) data).put(mapping.getTargetProp(), value);
            return data;
        }

        if (data instanceof List) {
            for (Object obj : (List) data) {
                if (obj instanceof Map) {
                    String value = getValue(dictItems, (String) ((Map) obj).get(mapping.getSourceProp()));
                    ((Map) obj).put(mapping.getTargetProp(), value);
                } else {
                    String value = getValue(dictItems, (String) ReflectUtil.getFieldValue(obj, mapping.getSourceProp()));
                    ReflectUtil.setFieldValue(obj, mapping.getTargetProp(), value);
                }
            }
            return data;
        }

        String value = getValue(dictItems, (String) ReflectUtil.getFieldValue(data, mapping.getSourceProp()));
        ReflectUtil.setFieldValue(data, mapping.getTargetProp(), value);

        return data;
    }


    private static String getValue(List<DictItem> dictItems, String code) {
        for (DictItem v : dictItems) {
            if (v.getCode().equals(code)) {
                return v.getName();
            }
        }
        return null;
    }

}
